package com.kunchi.platform.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kunchi.platform.model.device.DataItem;
import com.kunchi.platform.model.device.Device;
import com.kunchi.platform.model.device.DeviceMode;
import com.kunchi.platform.model.gateway.Gateway;
import com.kunchi.platform.service.DeviceService;
import com.kunchi.platform.service.LoginService;
import com.kunchi.platform.utils.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.List;

/**
 * Created by denniszhou on 2017/6/19.
 */
@Service("deviceService")
@PropertySource("classpath:config.properties")
public class DeviceServiceImpl implements DeviceService {
    @Resource
    LoginService loginService;

    @Autowired
    Environment env;

    @Override
    public List<Device> getDeviceList() throws IOException {
        String url = env.getRequiredProperty("BASE_URL") +
                "/devicelist?token={0}&hash={1}";
        String token = loginService.login();
        String response = HttpUtils.getHttpGetResponse(MessageFormat.format(
                url, token, env.getRequiredProperty("LOGIN_HASH")));
        String devices = JSONObject.parseObject(response).getString("data");
        return JSON.parseArray(devices, Device.class);
    }

    @Override
    public Device getDevice(String deviceId) throws IOException {
        String url = env.getRequiredProperty("BASE_URL") +
                "/device/{0}?token={1}&hash={2}";
        String token = loginService.login();
        String response = HttpUtils.getHttpGetResponse(MessageFormat.format(
                url, deviceId, token, env.getRequiredProperty("LOGIN_HASH")));
        String device = JSONObject.parseObject(response).getString("data");
        return JSON.parseObject(device, Device.class);
    }

    @Override
    public List<DeviceMode> getDeviceModeList() throws IOException {
        String url = env.getRequiredProperty("BASE_URL") +
                "/devicemodelist?token={0}&hash={1}";
        String token = loginService.login();
        String response = HttpUtils.getHttpGetResponse(MessageFormat.format(
                url, token, env.getRequiredProperty("LOGIN_HASH")));
        String devices = JSONObject.parseObject(response).getString("data");
        return JSON.parseArray(devices, DeviceMode.class);
    }

    @Override
    public List<DataItem> getDataItemList(String deviceId) throws IOException {
        String url = env.getRequiredProperty("BASE_URL") +
                "/dataitemlist/getDataItemList?token={0}&hash={1}&deviceid={2}";
        String token = loginService.login();
        String response = HttpUtils.getHttpGetResponse(MessageFormat.format(
                url, token, env.getRequiredProperty("LOGIN_HASH"), deviceId));
        String items = JSONObject.parseObject(response).getString("data");
        return JSON.parseArray(items, DataItem.class);
    }

    @Override
    public List<Gateway> getGateWayList() throws IOException {
        String url = env.getRequiredProperty("BASE_URL") + "/agentList?token={0}";
        String token = loginService.rentLogin();
        String response = HttpUtils.getHttpGetResponse(MessageFormat.format(url, token));
        String gateways = JSONObject.parseObject(response).getString("data");
        return JSON.parseArray(gateways, Gateway.class);
    }

}
